package com.burgeon.backend.Util.menu;





import com.alibaba.fastjson.JSONObject;

public class CommonUtil {

    /**
     * 网页授权获取openId
     * @Title: getOpenId
     * @Description: TODO
     * @param code
     * @return JSONObject
     */
    public static JSONObject getOpenId(String code) {
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
        String requestUrl = url.replace("APPID", WeChatInfo.WX_APPID).replace("SECRET", WeChatInfo.WX_APPSECRET).replace("CODE", code);

        JSONObject jsonObject =WeixinUtil.httpRequest(requestUrl, "GET", null);

//        String action = "https://api.weixin.qq.com/cgi-bin/user/get?access_token="
//                + accessToken;
//        try {
//            URL urlGet = new URL(requestUrl);
//            HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
//            http.setRequestMethod("GET"); // 必须是get方式请求
//            http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//            http.setDoOutput(true);
//            http.setDoInput(true);
//            System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
//            System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
//            http.connect();
//            InputStream is = http.getInputStream();
//            int size = is.available();
//            byte[] jsonBytes = new byte[size];
//            is.read(jsonBytes);
//            String result = new String(jsonBytes, "UTF-8");
//
//            JSONObject jsonObj = new JSONObject(result);
//
//           return jsonObj;
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        return jsonObject;
    }
}
